package org.dicker.controller;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dicker.biz.UserBiz;
import org.dicker.biz.impl.UserBizImpl;
import org.dicker.entity.MessageInfo;
import org.dicker.entity.UserInfo;
import org.lanqiao.mvc.entity.ModelAndView;
import org.lanqiao.mvc.entity.MultipartFile;
import org.lanqiao.mvc.entity.YockMvcAnnotation.Controller;
import org.lanqiao.mvc.entity.YockMvcAnnotation.RequestMapping;
import org.lanqiao.mvc.entity.YockMvcAnnotation.RequestParam;
import org.lanqiao.mvc.entity.YockMvcAnnotation.ResponseBody;
import org.lanqiao.mvc.entity.YockMvcAnnotation.ResponseDispatch;
import org.lanqiao.mvc.entity.YockMvcAnnotation.UploadFile;

@Controller("/user")
public class UserController {

	private UserBiz userBiz=new UserBizImpl();
	@RequestMapping(value = "/list.do") 
	@ResponseDispatch(value = "/WEB-INF/console/user/member-list.jsp")
	public ModelAndView index(
			HttpServletRequest req,
			HttpServletResponse res,
			@RequestParam(name = "username") String username,
			@RequestParam(name = "page") String page){
		ModelAndView mv = new ModelAndView();

		if(username==null){
			username="";
		}
		
		int pageSize=5;
		int newpage=0;
		if(page==null){
			page="1";
		}
		newpage=Integer.parseInt(page);
		
		List<UserInfo> userList = userBiz.selectAll(username, newpage, pageSize);
		mv.addObject("userList", userList);
		
		try {
			int totalcount = userBiz.selectCount(username);
			int totalpage=totalcount%pageSize==0 ? totalcount/pageSize : totalcount/pageSize+1;
			mv.addObject("totalpage", totalpage);
			mv.addObject("newpage", newpage);
			
			////////////////////
			//每个页码组显示5个
			int groupSize=5;
			
			//根据当前第几页求出位于第几组
			int currentGroup = (newpage-1)/groupSize + 1;
				
			//计算出当前这一组应该显示的页码范围
			//（startPage  ~  endPage）
			//（1  ~ 5）	   第1组
			//（6 ~ 10）	   第2组
			//（11 ~ 15）	   第3组
			//（16 ~ 20）	   第4组
			int startPage = (currentGroup-1)*groupSize+1;
			int endPage = currentGroup*groupSize;
			mv.addObject("startPage", startPage);
			mv.addObject("endPage", endPage);
			////////////////////
			
			
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		
		
		return mv;
	}
	
	@RequestMapping(value = "/delete.do") 
	@ResponseDispatch(value = "/WEB-INF/console/user/member-list.jsp")
	public ModelAndView delete(HttpServletRequest req,HttpServletResponse res){
		ModelAndView mv = new ModelAndView();
		int user_id = Integer.parseInt(req.getParameter("user_id"));
		int userdelete = userBiz.deleteuserid(user_id);
		mv.addObject("userdelete", userdelete);
		return mv;
	}
	
	/**修改页面*/
	@RequestMapping(value = "/edit.do") 
	@ResponseDispatch(value = "/WEB-INF/console/user/member-edit.jsp")
	public ModelAndView phone(HttpServletRequest req,HttpServletResponse res,
			@RequestParam(name = "wx_openid")String wx_openid){
		
		ModelAndView mv = new ModelAndView();
		
		try {
			UserInfo edit = userBiz.selectUserid(wx_openid);
			mv.addObject("edit", edit);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	@RequestMapping(value = "/update.do") 
	@ResponseBody
	public String update(HttpServletRequest req,HttpServletResponse res,
			@RequestParam(name = "username")String username,
			@RequestParam(name = "user_img")String user_img,
			@RequestParam(name = "user_id")int user_id){
		
		String str="";
		try {
			UserInfo userInfo = new UserInfo();
			userInfo.setUser_id(user_id);
			userInfo.setUser_name(username);
			userInfo.setUser_img(user_img);
			int update = userBiz.update_user(userInfo);
			str=update+"";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}


}
